﻿// Copyright 2010 Maxime Serrano <lastchronicler@gmail.com>
using System;
using System.IO;
using MCSharp.Network.Handlers;

namespace MCSharp.Network
{
    public class PacketHandlers
    {
        private static PacketHandler[] handlers = new PacketHandler[(int)CMSG.NumCMSG];

        public static void Init() {
            for (int i = 0; i < handlers.Length; ++i)
                handlers[i] = null;

            handlers[(int)CMSG.Handshake] = new HandshakeHandler();
            handlers[(int)CMSG.Login] = new LoginHandler();
            handlers[(int)CMSG.Chat] = new ChatHandler();
            handlers[(int)CMSG.Disconnect] = new DisconnectHandler();
            handlers[(int)CMSG.Move] = new MovementHandler();
            handlers[(int)CMSG.MoveLook] = new MoveLookHandler();
            handlers[(int)CMSG.Look] = new LookHandler();
            handlers[(int)CMSG.Bool] = new BoolHandler();
            handlers[(int)CMSG.Switch] = new SwitchHandler();
            handlers[(int)CMSG.ArmAnimation] = new ArmAnimationHandler();
            handlers[(int)CMSG.BlockDig] = new BlockDigHandler();
            handlers[(int)CMSG.BlockPlace] = new BlockPlaceHandler();
        }

        public static void HandlePacket(BinaryReader reader, Session session, Player player, byte opcode, byte lastopcode) {
            if (opcode > handlers.Length || handlers[opcode] == null)
            {
                if (opcode != 0)
                    Logger.Log("Unhandled opcode: {0} Last opcode: {1}", LogType.Debug, opcode, lastopcode);
                return;
            }

            handlers[opcode].Handle(reader, session, player);
        }
    }
}
